package config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import utils.encrypt.DecryptUtil;

import java.util.Properties;

public class DecryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    private static String PRIVATEKEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAL/IT3bzlSNjKQT4BfRBqrJ8e+yvaCrB0hZ4hg7WT7bPV0HhAVVD6J+/8Z9Folcjbl7L1eS0LCOt475iZb1egwnmwEcAASUl4vkUlEP7e2O/oMLm9P39fWv9/1umhGcXFinILxKC7/K2SeQfOBaGcNzVRUr0UOGOQoiX4va/KLylAgMBAAECgYACSdbLLCg2RfiNpmARA1wuwtC9ix/Qh7stYpDAakWvvBG9h8DjZu0PEO/svfY5GRAveVw58p+zekpLoIgejtcJIUuQdFN/+hEYkQ93AwfAJALgT0jVlnk5ZIHaK6nPN7YVnWzHlnoYCZZwZe758OVuTOVpcLSiKJRBiJE9dazVOQJBAOwpVm51lP78CQwzR9RH4cl6IPKZ9XtHWdlCbC3yjgc7UIGpRF0VJuMTCyaTpiYrqlokbmy+XVNgyJix/FAzY5kCQQDP5JmrODdxdL/ZUO/mjJucl0flBVLLb/x74I3K/omIxQWNmt3b43Sg0Gvv4gdpeQvl+s4TkKP4AjQXqiLDtsjtAkEAqWDMmNTEVE03wARXlC0qB3MMhnNd2VlHW8eEB0BcFVu5VgWs/oXZKivSZ4ymMeJHf/d5K40tKkvUE5A0QuFYoQJBAIFFdQiy/Ebggitv2L1T0a+domcW2FUIXcfp6kG/5WXWnY1gL3T8Cxm0gGm580HysyNewaYI2uzs8deFQ/jPQEECQAcCNQiQsCj/N0XWswYTrGEkbzCD5L8KwSxn99oJtXRRwQ2EYymc7IcAcr0PomH2NJf8IAKOu/2eZphforNPYwc=";

    @Override
    protected void processProperties(
            ConfigurableListableBeanFactory beanFactoryToProcess,
            Properties props) throws BeansException {
        String driverUrl = props.getProperty("jdbc.driverUrl");
        String username = props.getProperty("jdbc.user");
        String password = props.getProperty("jdbc.password");
        //解密jdbc.password属性值，并重新设置 
        try {
            driverUrl = DecryptUtil.decryptRSA(driverUrl, PRIVATEKEY);
            username = DecryptUtil.decryptRSA(username, PRIVATEKEY);
            password = DecryptUtil.decryptRSA(password, PRIVATEKEY);

            props.setProperty("jdbc.driverUrl", driverUrl);
            props.setProperty("jdbc.user", username);
            props.setProperty("jdbc.password", password);
        } catch (Exception e) {
            e.printStackTrace();
        }

        super.processProperties(beanFactoryToProcess, props);
    }
}
